
package com.youease.chris.action {
	import com.youease.chris.animate.Animation;
	import com.youease.chris.event.AnimateEvent;
	import com.youease.chris.event.TickEvent;
	import com.youease.chris.util.Tick;
	
	/**
	 * AnimateAction
	 *
	 * @author chris
	 *
	 * Dec 19, 2012
	 */
	public class AnimateAction extends BaseAction {
		private var animation:Animation;
		
		public function AnimateAction(animation:Animation)
		{
			super();
			this.animation = animation;
		}
		
		override public function execute():void {
			this.animation.addEventListener(AnimateEvent.ANIMATE_COMPLETE,onAnimationComplete);
			Tick.instance.addEventListener(TickEvent.TICK,tickHandler);
		}
		
		private function tickHandler(evt:TickEvent):void {
			this.animation.enterFrame(evt.interval/1000);
		}
		
		private function onAnimationComplete(evt:AnimateEvent):void {
			this.animation.removeEventListener(AnimateEvent.ANIMATE_COMPLETE,onAnimationComplete);
			Tick.instance.removeEventListener(TickEvent.TICK,tickHandler);
			this.complete();
		}
	}
}